#include "Projectile.h"
#include "Terrain.h"

Projectile::Projectile()
    : m_pMesh(NULL)
    , m_pTexture(NULL)
    , m_velocity(0, 0, 0)
    , m_speed(100.0f)
    , m_damage(25.0f)
    , m_lifetime(3.0f)
    , m_currentLifetime(0.0f)
    , m_isActive(true)
    , m_pTerrain(NULL)
{
}

Projectile::~Projectile()
{
    Cleanup();
}

bool Projectile::Initialize(LPDIRECT3DDEVICE9 device, const D3DXVECTOR3& startPos, const D3DXVECTOR3& direction, float speed)
{
    SetPosition(startPos);
    m_speed = speed;

    // Normalize direction and set velocity
    D3DXVec3Normalize(&m_velocity, &direction);
    m_velocity *= m_speed;

    // Create small sphere for projectile
    HRESULT hr = D3DXCreateSphere(device, 0.5f, 8, 8, &m_pMesh, NULL);
    if (FAILED(hr))
        return false;

    // Set bounding box (local space)
    m_boundingBox = AABB(
        D3DXVECTOR3(-0.5f, -0.5f, -0.5f),
        D3DXVECTOR3(0.5f, 0.5f, 0.5f)
    );

    // Create yellow texture for projectile
    hr = D3DXCreateTexture(device, 8, 8, 1, 0, D3DFMT_A8R8G8B8,
        D3DPOOL_MANAGED, &m_pTexture);
    if (SUCCEEDED(hr))
    {
        D3DLOCKED_RECT rect;
        m_pTexture->LockRect(0, &rect, NULL, 0);
        DWORD* pTexels = (DWORD*)rect.pBits;

        // Bright yellow/orange
        for (int i = 0; i < 8 * 8; i++)
        {
            pTexels[i] = D3DCOLOR_ARGB(255, 255, 200, 0);
        }
        m_pTexture->UnlockRect(0);
    }

    return true;
}

void Projectile::Update(float deltaTime)
{
    if (!m_isActive)
        return;

    // Update lifetime
    m_currentLifetime += deltaTime;
    if (m_currentLifetime >= m_lifetime)
    {
        m_isActive = false;
        return;
    }

    // Update position
    D3DXVECTOR3 newPos = m_position + m_velocity * deltaTime;

    // Check map boundaries
    if (newPos.x < 0.0f || newPos.x > 1000.0f ||
        newPos.z < 0.0f || newPos.z > 1000.0f)
    {
        m_isActive = false;
        return;
    }

    // Check terrain collision
    if (m_pTerrain)
    {
        float terrainHeight = m_pTerrain->GetHeight(newPos.x, newPos.z);
        if (newPos.y <= terrainHeight)
        {
            m_isActive = false;
            return;
        }
    }

    SetPosition(newPos);
}

void Projectile::Render(LPDIRECT3DDEVICE9 device)
{
    if (!device || !m_pMesh || !m_isActive)
        return;

    // Set world matrix
    D3DXMATRIX matWorld, matScale, matTrans;
    D3DXMatrixScaling(&matScale, 1.0f, 1.0f, 1.0f);
    D3DXMatrixTranslation(&matTrans, m_position.x, m_position.y, m_position.z);
    matWorld = matScale * matTrans;
    device->SetTransform(D3DTS_WORLD, &matWorld);

    // Set material (bright emissive)
    D3DMATERIAL9 material = {};
    material.Diffuse.r = material.Ambient.r = 1.0f;
    material.Diffuse.g = material.Ambient.g = 0.8f;
    material.Diffuse.b = material.Ambient.b = 0.0f;
    material.Diffuse.a = material.Ambient.a = 1.0f;
    material.Emissive.r = 0.5f;  // Make it glow
    material.Emissive.g = 0.4f;
    material.Emissive.b = 0.0f;
    device->SetMaterial(&material);

    // Set texture
    if (m_pTexture)
        device->SetTexture(0, m_pTexture);

    // Render mesh
    m_pMesh->DrawSubset(0);
}

void Projectile::Cleanup()
{
    if (m_pTexture)
    {
        m_pTexture->Release();
        m_pTexture = NULL;
    }
    if (m_pMesh)
    {
        m_pMesh->Release();
        m_pMesh = NULL;
    }
}
